package vn.cuhanh.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.cuhanh.BaseActivity;
import vn.cuhanh.MainActivity;
import vn.cuhanh.R;
import vn.cuhanh.bean.Award;
import vn.cuhanh.customview.ExpandableHeightGridView;
import vn.cuhanh.dialog.AwardHistoryActivity;
import vn.cuhanh.dialog.ViewGameCodeDialog;
import vn.cuhanh.models.Model_Award;
import vn.cuhanh.models.Model_Minigame;
import vn.cuhanh.utils.HelperFunc;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

public class AwardFragment extends BasicFragment {
	
	private static String GA_CATEGORY = "award_fragment";
	
	private String userName;
	public static int remainDndScore;
	private ArrayList<Award> listGift;
	private ArrayList<Award> listCode;
	private GiftGridAdapter giftGridAdapter;
	private CodeListAdapter codeListAdapter;
	private ProgressDialog progress;
	private View view;
	private View noNetworkScreen;
	private ScrollView contentWrapper;
	private View dailyDndScoreButton;
	private int currentTab = 0;
	private ExpandableHeightGridView giftGrid;
	private ListView codeList;
	private View tabControllGift;
	private View tabControllCode;
	private View tabGiftWrapper;
	private View tabCodeWrapper;
	
	public static AwardFragment newInstance(Context ctx) {

		return new AwardFragment(ctx);
	}

	public AwardFragment(Context ctx) {
		super(ctx);
	}

	public AwardFragment() {
		super();
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// setUserVisibleHint(true);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		loadAwardData(false);
	}
	
	public void showNoData() {
		listGift = null;
		listCode = null;
		contentWrapper.setVisibility(View.GONE);
		noNetworkScreen.setVisibility(View.VISIBLE);
	}
	
	public void setTabActive(View v) {
		v.setBackgroundColor(0xFFFFFFFF);
		View parent = (View)v.getParent();
		parent.setBackgroundColor(0xFFFFFFFF);
	}
	
	public void setTabInActive(View v) {
		v.setBackgroundResource(R.drawable.tab_controll_textview_background);
		View parent = (View)v.getParent();
		parent.setBackgroundColor(0xFF9D9D9D);
	}
	
	public void refreshUI() {
		contentWrapper.setVisibility(View.VISIBLE);
		noNetworkScreen.setVisibility(View.GONE);
		if(view != null) {
			TextView dndScore = (TextView)view.findViewById(R.id.award_total_score);
			dndScore.setText(String.valueOf(remainDndScore));
		}
		refreshButonDailyDndScore();
		refreshRemainDndScore();
		if(currentTab == 0) {
			setTabActive(tabControllGift);
			setTabInActive(tabControllCode);
			tabGiftWrapper.setVisibility(View.VISIBLE);
			tabCodeWrapper.setVisibility(View.GONE);
			if(giftGridAdapter != null) {
				giftGridAdapter.notifyDataSetChanged();
//				HelperFunc.setGridViewHeightBasedOnChildren(giftGrid, 2);
			}
		} else {
			setTabActive(tabControllCode);
			setTabInActive(tabControllGift);
			tabGiftWrapper.setVisibility(View.GONE);
			tabCodeWrapper.setVisibility(View.VISIBLE);
			if(codeListAdapter != null) {
				codeListAdapter.notifyDataSetChanged();
				HelperFunc.setListViewHeightBasedOnChildren(codeList);
			}
		}
	}
	
	public void loadAwardData(boolean forcedUpdate) {
		if(!MainActivity.isLogin || MainActivity.username == null || MainActivity.username.isEmpty()) {
			showNoData();
			return;
		}
		if(!forcedUpdate && userName != null && userName == MainActivity.username && listGift != null && listCode != null) {
			refreshUI();
			return;
		}
		userName = MainActivity.username;
		listGift = new ArrayList<Award>();
		listCode = new ArrayList<Award>();
		Thread thread = new Thread(new Runnable() {
			
			public void showNoData() {
				((Activity) ctx).runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						AwardFragment.this.showNoData();
					}
				});
			}
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String strResponse = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.getAllAwardUrl) + MainActivity.username);
				if(strResponse == null) {
					showNoData();
					return;
				}
				try {
					JSONObject jsonResponnse = new JSONObject(strResponse);
					if(jsonResponnse.getInt("result") != 1) {
						showNoData();
						return;
					}
					remainDndScore = jsonResponnse.getInt("remainScore");
					JSONArray jsonAwards = jsonResponnse.getJSONArray("data");
					for(int i = 0; i < jsonAwards.length(); i++) {
						JSONObject o = jsonAwards.getJSONObject(i);
						Award tmp = new Award(o.getInt("id"), o.getString("name"), o.getString("image"), o.getInt("point"), o.getInt("type"), o.getInt("created"), o.getBoolean("delivered"), o.getInt("active") == 1 ? true : false);
						if(tmp.getType() == 1) {
							listGift.add(tmp);
						} else {
							listCode.add(tmp);
						}
					}
					((Activity)ctx).runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							refreshUI();
						}
					});
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					showNoData();
				}
			}
		});
		thread.start();
	}
	
	public void initTabControll() {
		tabControllGift = view.findViewById(R.id.award_tabcontroll_gift);
		tabControllCode = view.findViewById(R.id.award_tabcontroll_code);
		tabGiftWrapper = view.findViewById(R.id.award_tab_gift_wrapper);
		tabCodeWrapper = view.findViewById(R.id.award_tab_code_wrapper);
		tabControllGift.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(ctx, GA_CATEGORY, "tab_controll_press", "tab_controll_press");
				loadTab(0);
			}
		});
		tabControllCode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(ctx, GA_CATEGORY, "tab_controll_press", "tab_controll_press");
				loadTab(1);
			}
		});
	}
	
	public void loadTab(int index) {
		currentTab = index;
		if(listGift == null || listCode == null) {
			loadAwardData(true);
		} else {
			refreshUI();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		// HelperFunc.unbindDrawables(view);
		// System.gc();
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		refreshRemainDndScore();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		try {
			view = inflater.inflate(R.layout.award, null);
			if (ctx == null || view == null) {
				if (isAdded()) {
					ctx = getActivity();
				} else {
					return view;
				}
			}
			if(MainActivity.userAccount != null) {
				TextView txtCharName = (TextView)view.findViewById(R.id.char_info_display_name);
				txtCharName.setText(ctx.getResources().getString(R.string.charName) + " " + MainActivity.userAccount.getCharName());
				TextView txtAccountName = (TextView)view.findViewById(R.id.char_info_username);
				txtAccountName.setText(ctx.getResources().getString(R.string.userName) + " " + MainActivity.username);
			}
			initGridAndButton();
			initTabControll();
			refreshRemainDndScore();
			refreshButonDailyDndScore();
			
			initNoNetwork();
			loadTab(currentTab);
			return view;
		} catch (InflateException e) {
			HelperFunc.readStackTrace(e);
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
		}
		return null;
	}
	
	public void refreshButonDailyDndScore() {
		if(Model_Minigame.isDailyClickAvailable(ctx, MainActivity.username)) {
//			dailyDndScoreButton.setBackgroundResource(R.drawable.get_point_btn_disable);
			dailyDndScoreButton.setClickable(true);
			dailyDndScoreButton.setEnabled(true);
		} else {
//			dailyDndScoreButton.setBackgroundResource(R.drawable.get_point_btn_disable);
			dailyDndScoreButton.setClickable(false);
			dailyDndScoreButton.setEnabled(false);
		}
	}
	
	public void refreshRemainDndScore() {
		TextView dndScore = (TextView)view.findViewById(R.id.award_total_score);
		dndScore.setText(String.valueOf(remainDndScore));
	}
	
	public void initGridAndButton() {
		giftGrid = (ExpandableHeightGridView)view.findViewById(R.id.award_tab_gift_grid);
		giftGridAdapter = new GiftGridAdapter(ctx, R.id.award_tab_gift_grid_item_image);
		giftGrid.setAdapter(giftGridAdapter);
		giftGrid.setExpanded(true);
		giftGrid.setCallback(new Runnable() { //after calculate grid height, scrollView scroll to bottom automatically.
			                                  //this force scrollView to scroll back to top.
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(contentWrapper != null) {
					contentWrapper.scrollTo(0, 0);
				}
			}
		});
		codeList = (ListView)view.findViewById(R.id.award_tab_code_list);
		codeListAdapter = new CodeListAdapter(ctx, R.id.award_tab_code_list_item_image);
		codeList.setAdapter(codeListAdapter);
		dailyDndScoreButton = view.findViewById(R.id.award_tab_gif_daily_dnd_score_button);
		dailyDndScoreButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(ctx, GA_CATEGORY, "daily_dnd_score_press", "daily_dnd_score_press");
				dailyDndScoreButton.setClickable(false);
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						final String response = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.dailyDndScoreHandlerUrl) + MainActivity.username + "&actionId=" + ctx.getResources().getInteger(R.integer.actionIdDailyScore));
						((Activity)ctx).runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								if(response == null) {
									HelperFunc.showSimpleAlertDialog(ctx, null, ctx.getResources().getString(R.string.no_network_2), ctx.getResources().getString(R.string.bt_ok_title), null, null, null, true);
									dailyDndScoreButton.setClickable(true);
									return;
								}
								try {
									JSONObject jsonRes = new JSONObject(response);
									int result = jsonRes.getInt("result");
									if(result == 0) { //unknown failure
										HelperFunc.showSimpleAlertDialog(ctx, null, ctx.getResources().getString(R.string.unknown_error), ctx.getResources().getString(R.string.bt_ok_title), null, null, null, true);
										dailyDndScoreButton.setClickable(true);
										return;
									}
									int lastClick = jsonRes.getInt("lastClick");
									int dailyScore = jsonRes.getInt("dailyScore");
									int totalScore = jsonRes.getInt("totalScore");
									int remainDndScore = jsonRes.getInt("remainDndScore");
									if(result == 1) {
										HelperFunc.showSimpleAlertDialog(ctx, null, ctx.getResources().getString(R.string.daily_dnd_score_success).replace("score", String.valueOf(dailyScore)), ctx.getResources().getString(R.string.bt_ok_title), null, null, null, true);
									}
									if(result == -1) { //user has clicked already
										HelperFunc.showSimpleAlertDialog(ctx, null, ctx.getResources().getString(R.string.daily_dnd_score_click_already), ctx.getResources().getString(R.string.bt_ok_title), null, null, null, true);
									}
									Model_Minigame.saveDailyScore(ctx, lastClick, dailyScore, MainActivity.username);
									BaseActivity.updateTotalScore(ctx, totalScore, null);
									AwardFragment.remainDndScore = remainDndScore;
									
									refreshUI();
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									HelperFunc.readStackTrace(e);
								}
							}
						});
					}
				});
				thread.start();
			}
		});
		
		Button btnShowHistory = (Button)view.findViewById(R.id.award_btn_show_history);
		btnShowHistory.setText(R.string.award_history);
		btnShowHistory.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(ctx, GA_CATEGORY, "show_history_btn_press", "show_history_btn_press");
				Intent intent = new Intent(ctx, AwardHistoryActivity.class);
				startActivity(intent);
			}
		});
	}
	
	public void initNoNetwork() {
		contentWrapper = (ScrollView)view.findViewById(R.id.award_content_wrapper);
		noNetworkScreen = view.findViewById(R.id.award_no_network);
		noNetworkScreen.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				loadAwardData(true);
			}
		});
	}
	
	public void handleAddGameItemResult(int result, Award award) {
		int index = listGift.indexOf(award);
		if(result != -15) { //no network
			AwardHistoryActivity.listAwardHistory = null;
			AwardHistoryActivity.currentPage = 0;
		}
		
		switch (result) {
		case 1: //success
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_success), ctx.getResources().getString(R.string.award_recieve_gift_success).replace("award_name", award.getName()), null, null, null, null, true);
			if(index != -1) {
				listGift.get(index).setDelivered(true);
			}
			break;
		case 0: //fail, unknown reason
		case -3: //num is not corrected (num>0)
		case -4: //serverid not exists
		case -6: //itemID is invalid 
		case -7: //Sig validation failure
		case -8: //Exceed The Allowable Number Of Send
		case -11: //parameter format error
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_unknown_error), null, null, null, null, true);
			break;
		case -1: //world server maintain
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_world_server_maintain), null, null, null, null, true);
			break;
		case -2: //accountid not exist on account server
		case -5: //accountid not exists on the world server
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_account_not_exist), null, null, null, null, true);
			break;
		case -12: //user score is not enough
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_dnd_not_enough), null, null, null, null, true);
			break;
		case -13: //award does not exist or is inactive
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_not_exist), null, null, null, null, true);
			if(index != -1) {
				listGift.get(index).setActive(false);
			}
			break;
		case -14: //user has recieved already
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.award_recieve_gift_delivered_already), null, null, null, null, true);
			if(index != -1) {
				listGift.get(index).setDelivered(true);
			}
			break;
		case -15: //timeout
			HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(R.string.no_network_2), null, null, null, null, true);
			break;
		default:
			break;
		}
	}

	public void requestAddGameItem(final Award award) {
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String url = ctx.getResources().getString(R.string.addGameItemUrl)
								.replace("user_name", MainActivity.username)
								.replace("passport_id", String.valueOf(MainActivity.userId))
								.replace("award_id", String.valueOf(award.getId()));
				Log.d("award", "Url: " + url);
				String response = HelperFunc.getDataFromSv(url, 20000);
				Log.d("award", String.valueOf(response));
				if(progress != null) {
					progress.dismiss();
				}
				int result = -15; //network timeout
				if(response != null) {
					try {
						JSONObject jsonRes = new JSONObject(response);
						result = jsonRes.getInt("result");
						remainDndScore = jsonRes.getInt("remainDndScore");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						result = 0;
						HelperFunc.readStackTrace(e);
					}
				}
				final int tmpResult = result;
				((Activity)ctx).runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						handleAddGameItemResult(tmpResult, award);
						refreshUI();
					}
				});
			}
		});
		thread.start();
	}
	
	public class GiftGridAdapter extends ArrayAdapter<Award> {

		public GiftGridAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public int getCount() {
			if(listGift == null) {
				return 0;
			}
			return listGift.size();
		}
		
		@Override
		public View getView(int position, View v, ViewGroup parent) {
			if(v == null) {
				LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.award_tab_gift_grid_item, null);
			}
			final Award obj = listGift.get(position);
			if(obj == null) {
				return v;
			}
			ImageView giftImage = (ImageView)v.findViewById(R.id.award_tab_gift_grid_item_image);
			TextView txtName = (TextView)v.findViewById(R.id.award_tab_gift_grid_item_description);
			TextView txtPoint = (TextView)v.findViewById(R.id.award_tab_gift_grid_item_score);
			UrlImageViewHelper.setUrlDrawable(giftImage, obj.getImage());
			txtName.setText(obj.getName());
			txtPoint.setText(ctx.getResources().getString(R.string.award_tab_gift_grid_item_score_text).replace("score", String.valueOf(obj.getPoint())));
			
			if(remainDndScore < obj.getPoint() || !obj.isActive()) {
				v.setEnabled(false);
			} else {
				v.setEnabled(true);
				v.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						HelperFunc.trackEvent(ctx, GA_CATEGORY, "add_game_item_btn_press", "add_game_item_btn_press");
						progress = ProgressDialog.show(AwardFragment.this.ctx, null,
								AwardFragment.this.ctx.getResources().getString(R.string.award_progress_dialog_message));
						requestAddGameItem(obj);
					}
				});
			}
			return v;
		}
	}
	
	public class CodeListAdapter extends ArrayAdapter<Award> {

		public CodeListAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			// TODO Auto-generated constructor stub
		}
		
		public int getCount() {
			if(listCode == null) {
				return 0;
			}
			return listCode.size();
		}
		
		public void showCode(final String codeValue) {
			((Activity)ctx).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Intent intent = new Intent(AwardFragment.this.ctx, ViewGameCodeDialog.class);
					intent.putExtra("codeValue", codeValue);
					startActivity(intent);
				}
			});
		}
		
		public void showErrorMessage(final int messageCode) {
			((Activity)ctx).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					HelperFunc.showSimpleAlertDialog(ctx, ctx.getResources().getString(R.string.award_recieve_gift_title_fail), ctx.getResources().getString(messageCode), null, null, null, null, true);
				}
			});
		}
		
		public View getView(int position, View v, ViewGroup parent) {
			if(v == null) {
				LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.award_tab_code_list_item, null);
			}
			final Award obj = listCode.get(position);
			if(obj == null) {
				return v;
			}
			ImageView imageView = (ImageView) v.findViewById(R.id.award_tab_code_list_item_image);
			UrlImageViewHelper.setUrlDrawable(imageView, obj.getImage());
			TextView txtName = (TextView) v.findViewById(R.id.award_tab_code_list_item_name);
			txtName.setText(obj.getName());
			TextView txtCost = (TextView) v.findViewById(R.id.award_tab_code_list_item_cost);
			txtCost.setText(ctx.getResources().getString(R.string.award_tab_gift_grid_item_score_text).replace("score", String.valueOf(obj.getPoint())));
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(final View v) {
					// TODO Auto-generated method stub
					HelperFunc.trackEvent(ctx, GA_CATEGORY, "get_code_btn_press", "get_code_btn_press");
					v.setClickable(false);
					Thread thread = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							String codeValue = Model_Award.getScoreFromDatabase(ctx, obj.getId(), MainActivity.username);
							if(codeValue != null) {
								v.setClickable(true);
								showCode(codeValue);
								return;
							}
							//Get code from server
							String response = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.getGameCodeUrl)
									.replace("user_name", MainActivity.username)
									.replace("code_id", String.valueOf(obj.getId()))
									.replace("campaign_created_date", String.valueOf(obj.getCreated())));
							if(response == null) {
								v.setClickable(true);
								showErrorMessage(R.string.no_network_2); //no network
								return;
							}
							try {
								JSONObject jsonRes = new JSONObject(response);
								int result = jsonRes.getInt("result");
								if(jsonRes.has("remainDndScore")) {
									remainDndScore = jsonRes.getInt("remainDndScore");
								}
								if(result == 0) {
									v.setClickable(true);
									showErrorMessage(R.string.unknown_error); //unknown error
									return;
								}
								if(result == -1) { //out of code
									((Activity)ctx).runOnUiThread(new Runnable() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											HelperFunc.showSimpleAlertDialog(ctx, 
													ctx.getResources().getString(R.string.alert_fail_title), 
													ctx.getResources().getString(R.string.award_out_of_code_message), 
													ctx.getResources().getString(R.string.close), null, null, null, true);
										}
									});
								}
								if(result == -2) {// User did not reach level 8
									((Activity)ctx).runOnUiThread(new Runnable() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											HelperFunc.showSimpleAlertDialog(ctx, 
													ctx.getResources().getString(R.string.alert_fail_title), 
													ctx.getResources().getString(R.string.award_level_not_enough), 
													ctx.getResources().getString(R.string.close), null, null, null, true);
										}
									});
								}
								if(result == -3) {// Char not found
									((Activity)ctx).runOnUiThread(new Runnable() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											HelperFunc.showSimpleAlertDialog(ctx, 
													ctx.getResources().getString(R.string.alert_fail_title), 
													ctx.getResources().getString(R.string.award_char_not_found), 
													ctx.getResources().getString(R.string.close), null, null, null, true);
										}
									});
								}
								if(result == -12) { //not enough dnd score
									((Activity)ctx).runOnUiThread(new Runnable() {
										
										@Override
										public void run() {
											// TODO Auto-generated method stub
											HelperFunc.showSimpleAlertDialog(ctx, 
													ctx.getResources().getString(R.string.alert_fail_title), 
													ctx.getResources().getString(R.string.award_recieve_gift_dnd_not_enough), 
													ctx.getResources().getString(R.string.close), null, null, null, true);
										}
									});
								}
								if(result == 1) {
									JSONObject data = jsonRes.getJSONObject("data");
									codeValue = data.getString("code");
									Model_Award.insert(ctx, MainActivity.username, obj.getId(), codeValue);
									showCode(codeValue);
								}
								((Activity)ctx).runOnUiThread(new Runnable() {
									
									@Override
									public void run() {
										// TODO Auto-generated method stub
										refreshUI();
									}
								});
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							v.setClickable(true);
						}
					});
					thread.start();
				}
			});
			return v;
		}
	}
}
